D:\cs151\chap80>java Square Enter an integer: Rats Exception in thread "main" java.lang.NumberFormatException: Rats at java.lang.Integer.parseInt(Integer.java:409) at java.lang.Integer.parseInt(Integer.java:458) at Square .main(NFException.java:18) D:\cs151\chap80>
The user entered Rats which parseInt
could
not convert to an integer.
Nothing is wrong with the program.
The problem is that parseInt
cannot convert
"Rats" into an int
.
When parseInt
found the problem it
threw a NumberFormatException
.
The Java run-time system caught the exception,
halted the program, and printed the error messages.
An exception is a problem that occurs
when a program is running.
Often the problem is caused by circustances outside
the control of the program,
such as bad user input.
When an exception occurs,
the Java virtual machine creates an object
of class Exception which holds information about the problem.
A Java program itself may
catch an exception.
It can then use the Exception
object
to recover from the problem.
An error, also, is a problem that occurs when a program is running. An error is represented by an object of class Error. But an error is too severe for a program to handle. The program must stop running.